package com.cimcitech.printertestdemo.receiver;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.util.Log;

import com.cimcitech.printertestdemo.MainActivity;
import com.cimcitech.printertestdemo.utils.PrintUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import javax.security.auth.login.LoginException;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static com.cimcitech.printertestdemo.utils.USBHelper.ACTION_USB_PERMISSION;

/**
 * USB 设备 监听广播
 *
 * @author by ken on 2019.5.6
 */
public class USBDeviceReceiver extends BroadcastReceiver {
    private static final String TAG = "USBDeviceReceiver";

    private Context mContext;

    public USBDeviceReceiver() {
    }


    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e(TAG, "onReceive: " + intent.getAction());
        String action = intent.getAction();

        PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
        if (ACTION_USB_PERMISSION.equals(action)) {
            synchronized (this) {
                UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                Log.e(TAG, "onReceive: " + device);

                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, true)) {

                    if (device != null) {
                        //call method to set up device communication
                        Log.e(TAG, "permission granted for usbaccessory " + device);

                        UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
                        manager.requestPermission(device, mPermissionIntent);

                        // 获取设备
                        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();

                        Log.e(TAG, "onReceive: deviceList.size()" + deviceList.size());


                        for (Map.Entry<String, UsbDevice> entry : deviceList.entrySet()) {

                            Log.e(TAG, "onReceive: getKey" + entry.getKey());
                            Log.e(TAG, "onReceive: getValue " + entry.getValue());


                            if (Objects.requireNonNull(entry.getValue()).getProductId() == PrintUtils.PRODUCE_ID
                                    && Objects.requireNonNull(entry.getValue()).getVendorId() == PrintUtils.VENDER_ID) {
                                Intent intent1 = new Intent(context, MainActivity.class);
                                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                context.startActivity(intent1);

                            }


                        }


                    }
                } else {
                    Log.e(TAG, "permission denied for device " + device);
                }
            }
        }
    }
}
